package com.logic.modular.gameCore.cultivate.hero.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.comment.pojo.domain.logic.core.CoreHeroStar;
import lombok.extern.slf4j.Slf4j;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logic.modular.gameCore.cultivate.hero.mapper.CoreHeroStarMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import static com.logic.modular.gameCore.CoreCacheInfo.infoCache;


/**
 * 武将星级表 PS：成长值使用的时候需要除以100影响到的属性值，进行四舍五入取整（满0.5进位）(CoreHeroStar)表服务实现类
 *
 * @author cxy
 * @since 2024-08-05 16:14:02
 */
@Slf4j
@Service
public class CoreHeroStarServiceImpl extends ServiceImpl<CoreHeroStarMapper, CoreHeroStar>
        implements IService<CoreHeroStar> , CoreHeroStarService
{
    private static final Cache<String, Map<String, CoreHeroStar>> cache = Caffeine.newBuilder().build();

    @Resource
    private CoreHeroStarMapper coreHeroStarMapper;

    @PostConstruct
    private void init() {
        CoreHeroStar initSelect = CoreHeroStar.builder().build();
        List<CoreHeroStar> list = this.list(new LambdaQueryWrapper<>(initSelect));
        log.info("开始缓存武将星级表 PS：成长值使用的时候需要除以100影响到的属性值，进行四舍五入取整（满0.5进位）数据,数据量为:{}", list.size());
        cache.put(
                "coreHeroStar",
                list.stream().collect(Collectors.toMap(item -> item.getHeroId() + "," + item.getStar(), CoreHeroStar::init))
        );
        log.info("武将星级表 PS：成长值使用的时候需要除以100影响到的属性值，进行四舍五入取整（满0.5进位）数据缓存完成");
    }

    public List<CoreHeroStar> listCache() {
        Map<String, CoreHeroStar> values = cache.getIfPresent("coreHeroStar");
        if (Objects.isNull(values)) {
            throw new RuntimeException("武将星级表 PS：成长值使用的时候需要除以100影响到的属性值，进行四舍五入取整（满0.5进位）数据为空");
        }
        return new ArrayList<>(values.values());
    }

    public Map<String, CoreHeroStar> mapCache() {
        Map<String, CoreHeroStar> values = cache.getIfPresent("coreHeroStar");
        if (Objects.isNull(values)) {
            throw new RuntimeException("武将星级表 PS：成长值使用的时候需要除以100影响到的属性值，进行四舍五入取整（满0.5进位）数据为空");
        }
        return values;
    }

    public static CoreHeroStar getCache(Long heroId, Integer star) {
        Map<String, CoreHeroStar> values = cache.getIfPresent("coreHeroStar");
        if (Objects.isNull(values)) {
            throw new RuntimeException("武将星级表 PS：成长值使用的时候需要除以100影响到的属性值，进行四舍五入取整（满0.5进位）数据为空");
        }
        return values.get(heroId + "," + star);
    }

    public String getProp(Long heroId) {
        return getCache(heroId, 1).getLevelUpCost();
    }

    @Override
    public int getMaxStar() {
        String key = String.format("%s", "heroMaxStar");
        Integer heroMaxStar = (Integer) infoCache.getIfPresent(key);
        if (heroMaxStar == null) {
            heroMaxStar = coreHeroStarMapper.getMaxStar();
            infoCache.put(key, heroMaxStar);
        }
        return heroMaxStar;
    }
}
